#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys

from flask import Response
from flask_restful import reqparse, Resource
from src.formats.insensitive_argument import CaseInsensitiveArgument
from src.formats import MapServiceManager
from src.routers.GetDataFromManager import Get3DTilesJson, Get3DTilesFiles, Get3DTilesFiles_f1


sys.path.append("../..")

# 全局变量
mapServiceManager = MapServiceManager.mapServiceManager


class C3DTilesJson(Resource):
    def __init__(self):
        print('init C3DTilesJson')

    def get(self, servicename, filename):
        resp = ''
        try:
            resp = Response(Get3DTilesJson(servicename, filename), status=200, mimetype='application/json')
        except Exception as e:
            print(e)
            resp = Response(servicename  + ' Not Found',
                            status=404,
                            mimetype='text/javascript')

        # resp.headers['Content-Encoding'] = 'gzip'
        resp.headers['Access-Control-Allow-Origin'] = '*'
        resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
        resp.headers['Access-Control-Allow-Credentials'] = 'GET,POST,OPTIONS'

        return resp


class C3DTilesFiles(Resource):
    def __init__(self):
        print('init C3DTilesFiles')

    def get(self, servicename, tilename, filename):
        resp = ''
        try:
            data = Get3DTilesFiles(servicename, tilename, filename)
            fFormat = filename.split('.')[1]
            print(fFormat)
            mimetype = 'html/text'
            if fFormat == 'json':
                mimetype = 'application/json'
            elif fFormat == 'b3dm':
                mimetype = 'application/octet-stream'
            resp = Response(data, status=200, mimetype=mimetype)
        except Exception as e:
            print(e)
            resp = Response(servicename + ' file ' + filename + ' Not Found',
                            status=404,
                            mimetype='text/javascript')
        # resp.headers['Content-Encoding'] = 'gzip'

        resp.headers['Access-Control-Allow-Origin'] = '*'
        resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
        resp.headers['Access-Control-Allow-Credentials'] = 'GET,POST,OPTIONS'

        return resp

# 无Data目录情况下的文件读取
class C3DTilesFiles_f1(Resource):
    def __init__(self):
        print('init C3DTilesFiles_f1')

    def get(self, servicename, folder1, folder2, filename):
        resp = ''
        try:
            data = Get3DTilesFiles_f1(servicename, folder1, folder2, filename)
            fFormat = filename.split('.')[1]
            print(fFormat)
            mimetype = 'html/text'
            if fFormat == 'json':
                mimetype = 'application/json'
            elif fFormat == 'b3dm':
                mimetype = 'application/octet-stream'
            resp = Response(data, status=200, mimetype=mimetype)
        except Exception as e:
            print(e)
            resp = Response(servicename + ' file ' + filename + ' Not Found',
                            status=404,
                            mimetype='text/javascript')
        # resp.headers['Content-Encoding'] = 'gzip'

        resp.headers['Access-Control-Allow-Origin'] = '*'
        resp.headers['Access-Control-Allow-Headers'] = 'Content-Type'
        resp.headers['Access-Control-Allow-Credentials'] = 'GET,POST,OPTIONS'

        return resp